﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Authorize;
using Project_KhuyenMaiVang.DAO;
using Project_KhuyenMaiVang.Models;

namespace Project_KhuyenMaiVang.Controllers
{
    [MyAuthorize]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult QuanLyTinKhuyenMai()
        {
            TinKhuyenMaiDAO tinKhuyenMaiDAO = new TinKhuyenMaiDAO();
            List<TinKhuyenMaiDTO> dsTinKhuyenMai = tinKhuyenMaiDAO.getAllSaleOffNews();
            SanPhamDAO sanPhamDAO = new SanPhamDAO();
            List<SanPhamDTO> sanPhamList = new List<SanPhamDTO>();
            for (int i = 0; i < dsTinKhuyenMai.Count; i++)
            {
                sanPhamList.Add(sanPhamDAO.getSanPham(dsTinKhuyenMai[i].MaSanPham));
            }
            ViewBag.SanPhamList = sanPhamList;
            return View(dsTinKhuyenMai);
        }

        public ActionResult QuanLyDonDatHang()
        {
            DonDatHangDAO donDatHangDAO = new DonDatHangDAO();
            List<DonDatHangDTO> dsDonDatHangDTO = donDatHangDAO.getAllOrders();
            List<DonDatHang> dsDonDatHang = new List<DonDatHang>();
            for (int i = 0; i < dsDonDatHangDTO.Count; i++)
            {
                DonDatHang temp = new DonDatHang();
                temp.MaDonDatHang = dsDonDatHangDTO[i].MaDonDatHang;
                temp.NguoiDatHang = (new AccountDAO()).findWithIDAccount(dsDonDatHangDTO[i].IDAccount).UserName;
                temp.TenSanPham = (new TinKhuyenMaiDAO()).getDetailOfSaleOffNews(dsDonDatHangDTO[i].MaTinKhuyenMai).ThongTinTomTat;
                temp.SoLuong = dsDonDatHangDTO[i].SoLuong;
                temp.TinhTrangDonHang = (new TinhTrangDonDatHangDAO()).getOrderState(dsDonDatHangDTO[i].MaTinhTrangDonDatHang);
                dsDonDatHang.Add(temp);
            }
            return View(dsDonDatHang);
        }
        public ActionResult QuanLyDoanhNghiep()
        {
            //Show all doanh nghiep
            DoanhNghiepDAO doanhNghiepDAO = new DoanhNghiepDAO();
            List<DoanhNghiepDTO> dsDoanhNghiep = doanhNghiepDAO.getAllDoanhNghiep();
            return View(dsDoanhNghiep);
        }

        public ActionResult QuanLyComment()
        {
            CommentDAO commentDAO = new CommentDAO();
            List<CommentDTO> dsComment = commentDAO.getAllComments();
            AccountDAO acc = new AccountDAO();
            List<AccountDTO> accList = new List<AccountDTO>();
            List<TinKhuyenMaiDTO> tinKhuyenMaiList = new List<TinKhuyenMaiDTO>();
            TinKhuyenMaiDAO tinKhuyenMaiDAO = new TinKhuyenMaiDAO();
            for (int i = 0; i < dsComment.Count; i++)
            {
                accList.Add(acc.getAccount(dsComment[i].IDAccount));
                tinKhuyenMaiList.Add(tinKhuyenMaiDAO.getDetailOfSaleOffNews(dsComment[i].MaTinKhuyenMai));
            }
            ViewBag.AccountList = accList;
            ViewBag.TinKMList = tinKhuyenMaiList;
            return View(dsComment);
        }
        public ActionResult ThongKe()
        {
            return View();
        }

        public ActionResult QuanLyThanhVien()
        {
            MemberDAO memDAO = new MemberDAO();
            List<MemberDTO> memList = memDAO.GetAllMember();
            return View(memList);
        }

        [HttpGet]
        public ActionResult EditThanhVien(int ID)
        {
            MemberDAO memDAO = new MemberDAO();
            MemberDTO memDTO = memDAO.GetInfor(ID);
            return View(memDTO);
        }

        [HttpPost]
        public ActionResult EditThanhVien(MemberDTO input)
        {
            MemberDAO memDAO = new MemberDAO();
            memDAO.UpdateMember(input);
            return RedirectToAction("QuanLyThanhVien");
        }

        public ActionResult DeleteThanhVien(int ID)
        {
            MemberDAO memDAO = new MemberDAO();
            memDAO.DeleteMember(ID);
            return RedirectToAction("QuanLyThanhVien");
        }

        public ActionResult EditTinKhuyenMai(int id)
        {
            TinKhuyenMaiDAO tinKhuyenMaiDAO = new TinKhuyenMaiDAO();
            TinKhuyenMaiDTO tinKhuyenMaiInfo = tinKhuyenMaiDAO.getDetailOfSaleOffNews(id);
            return View(tinKhuyenMaiInfo);
        }
        [HttpPost]
        public ActionResult EditTinKhuyenMai(TinKhuyenMaiDTO tinKhuyenMai)
        {
            TinKhuyenMaiDAO tinKhuyenMaiDAO = new TinKhuyenMaiDAO();
            tinKhuyenMaiDAO.update(tinKhuyenMai);
            return RedirectToAction("QuanLyTinKhuyenMai", "Admin");
        }
        [HttpGet]
        public ActionResult EditDoanhNghiep(int id)
        {
            DoanhNghiepDAO doanhNghiepDAO = new DoanhNghiepDAO();
            DoanhNghiepDTO doanhNghiepInfo = doanhNghiepDAO.getBussinessInfo(id);
            return View(doanhNghiepInfo);
        }
        [HttpPost]
        public ActionResult EditDoanhNghiep(DoanhNghiepDTO doanhNghiep)
        {
            DoanhNghiepDAO doanhNghiepDAO = new DoanhNghiepDAO();
            
            return RedirectToAction("QuanLyDoanhNghiep");
        }

        public ActionResult XemChiTietSanPham(int idSanPham)
        {
            SanPhamDAO sanPhamDAO = new SanPhamDAO();
            SanPhamDTO sanPhamDTO = sanPhamDAO.getSanPham(idSanPham);
            return View(sanPhamDTO);
        }

        public ActionResult ChiTietLienHe(int maLienHe)
        {
            LienHeDAO lienHeDAO = new LienHeDAO();
            LienHeDTO lienHe = lienHeDAO.getLienHe(maLienHe);
            return View(lienHe);
        }

        [HttpGet]
        public ActionResult EditLienHeDoanhNghiep(int idLienHe, int idDoanhNghiep)
        {
            LienHeDAO lienHeDAO = new LienHeDAO();
            LienHeDTO lienHe = lienHeDAO.getLienHe(idLienHe);
            ViewBag.IDDoanhNghiep = idDoanhNghiep;
            return View(lienHe);
        }
        [HttpPost]
        public ActionResult EditLienHeDoanhNghiep(LienHeDTO lienHe, string IDDoanhNghiep)
        {
            LienHeDAO lienHeDAO = new LienHeDAO();
            lienHeDAO.updateInfo(lienHe.MaLienHe, lienHe.SoDienThoai,lienHe.Email, lienHe.Facebook, lienHe.DiaChi);
            int idDN = Int32.Parse(IDDoanhNghiep);
            return RedirectToAction("EditDoanhNghiep", new {id=idDN });
        }
    }
}
